package com.ebor.utils.exception;

import com.ebor.utils.common.HttpStatus;
import com.ebor.utils.common.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletResponse;

/**
 * @author C
 */
@Slf4j
@RestController
public class HttpErrorHandler implements ErrorController {

    private static final String ERROR_PATH = "/error";

    @RequestMapping(value = ERROR_PATH)
    public Response<?> error(HttpServletResponse response) {

        HttpStatus httpStatus;
        switch (response.getStatus()) {
            case 400:
                httpStatus = HttpStatus.BAD_REQUEST;
                break;
            case 404:
                httpStatus = HttpStatus.NOT_FOUND;
                break;
            default:
                httpStatus = HttpStatus.UNKNOWN_EXCEPTION;
                break;
        }
        throw new SysRuntimeException(httpStatus);
    }

    @Override
    public String getErrorPath() {
        return ERROR_PATH;
    }

}